﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Jzy.Model;
using Jzy.BLL.Mail;
using Jzy.Model.Mail;

using System.Collections.Generic;
using Jzy.Core.Common.Web;
using Frame.Lib.Core.Attach;
using Frame.Lib.Utilities;
using Frame.Lib.Common;
using Frame.Lib.Enums;
using Frame.Lib.Core.Module;
using Jzy.DAL;

public partial class WebMail_Edit : PageEdit
{
    private WebMailBO _mail;
    private WriteMailMode _mode;
    private string MailID
    {
        get
        {
            return Request["ID"];
        }
    }
    private string _content = "";

    protected string Content
    {
        get
        {
            return _content.Replace("'", "\\'").Replace("\r\n", "<br/>");
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        _mode = (WriteMailMode)int.Parse(Request["Mode"]);
        _mail = new WebMailBO(AppSession.UserID);
        if (!IsPostBack)
        {
            BindAccount();
            if (MailID != null)
            {
                DispInfo();
            }
        }
        AddAttr();
        this.AllowPageScroll();
    }

    private void BindAccount()
    {
        WebMailAccountBO account = new WebMailAccountBO(AppSession.UserID);
        account.BindDrp(drpAccount);         
    }

    private void DispInfo()
    {
        WebMailInfo info = _mail.GetInfo(MailID);
       

        switch (_mode)
        {
            case WriteMailMode.Reply://回复
                ToAddr.Text = info.FromAddr.ToString();
                Name.Text = "Re:" + info.Name;
                _content = "<div style='padding-left:10px;'><br/><br/><br/>========" + info.InputTime + "&nbsp;你在来信中写道========<br/>" +
                          "<div style='padding-left:10px;'><div style='border-left:solid 1px #a0a0a0;padding-left:10px;'>" + info.Body.ToString() + "</div></div></div>";
                break;
            case WriteMailMode.Forword://转发
                Name.Text = "Fw:" + info.Name;
                _content = "<div style='padding-left:10px;'><br/><br/><br/>========下面是转发邮件========<br/><br/>" +
                          "原邮件发件人：\"" + info.FromName + "<" + info.FromAddr  + ">\"<br/>" +
                          "原邮件收件人：" + info.ToAddr + "<br/>" +
                          "原邮件发送时间：" + info.InputTime + "<br/><br/>" +
                          "<div style='padding-left:10px;'><div style='border-left:solid 1px #a0a0a0;padding-left:10px;'>" + info.Body.ToString() + "</div></div>" +
                          "===============================</div>";     
                //附件需要转发，暂未实现
                break;
            case WriteMailMode.Draft://草稿
                EntityUtil.SetControl(pnlMain, info);
                _content = info.Body;
                break;
        }
        if (_mode == WriteMailMode.Forword || _mode == WriteMailMode.Draft)
        {
            AttachBO attach = new AttachBO(OTableName.WebMailAttach);
            IEnumerable<AttachInfo> list = attach.GetList(int.Parse(MailID));
            rptAttach.DataSource = list;
            rptAttach.DataBind();
        }
    }

    private void AddAttr()
    {
        this.Form.Attributes.Add("enctype", "multipart/form-data");
        JSUtil.ClickBtn(lnkSend, btnSend);
        JSUtil.ClickBtn(lnkSave, btnSave);
    }

    protected void btnSend_Click(object sender, EventArgs e)
    {
        WebMailInfo info = GetInfo();

        WebMailAccountBO account = new WebMailAccountBO(AppSession.UserID);
        WebMailAccountInfo accountInfo = account.GetInfo(drpAccount.SelectedValue);
        

        List<int> lstOldAttachIDs = new List<int>();//转发的附件
        foreach (RepeaterItem item in rptAttach.Items)
        {
            if (item.ItemType == ListItemType.AlternatingItem || item.ItemType == ListItemType.Item)
            {
                HiddenField hidID = (HiddenField)item.FindControl("hidID");
                CheckBox chkSelect = (CheckBox)item.FindControl("chkSelect");
                if (chkSelect.Checked)
                {
                    lstOldAttachIDs.Add(int.Parse(hidID.Value));
                }
            }
        }

        bool flag = _mail.Send(accountInfo, info, Request.Files, lstOldAttachIDs, _mode);
        

        if (flag)
        {
            ModuleBO module = new ModuleBO();
            int moduleID = (int)module.GetIDByCode(ModuleCode.WebReceiveBox);
            JSUtil.SelectModule(moduleID, "发送成功");

        }
        else
        {
            JSUtil.Alert("发送失败");
            _content = info.Body;
        }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        WebMailInfo info = GetInfo();

        WebMailAccountBO account = new WebMailAccountBO(AppSession.UserID);
        WebMailAccountInfo accountInfo = account.GetInfo(drpAccount.SelectedValue);
        info.AccountAddr = accountInfo.MailAddr;
        info.Status = (int)MailStatus.HaveRead;

        bool flag = _mail.SaveToDraft(info, Request.Files);
        if (flag)
        {
            ModuleBO module = new ModuleBO();
            int moduleID = (int)module.GetIDByCode(ModuleCode.WebDraftBox);
            JSUtil.SelectModule(moduleID, "保存草稿成功");
        }
        else
        {
            JSUtil.Alert("保存草稿失败");
            _content = info.Body;
        }
    }

    private WebMailInfo GetInfo()
    {
        WebMailInfo info = new WebMailInfo();
        EntityUtil.SetEntity(pnlMain, info);
        info.UserID = AppSession.UserID;

        //只有来源于草稿箱的，保存时才更新，其余全为增加，如来源于回复或转发也要保存ＩＤ以便于改变状态。
        if (_mode == WriteMailMode.Draft || _mode == WriteMailMode.Forword || _mode == WriteMailMode.Reply)
        {
            info.ID = int.Parse(MailID);
        }
        info.InputTime = DateTime.Now;
        info.Body = Request.Form["content"];        
        return info;
    }
}
